package com.bawei.shopping;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.room.good.GoodDatabase;
import com.blankj.utilcode.util.ToastUtils;
import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;


@Route(path = RouterPath.App.PAGER_HOME)
public class MainActivity extends AppCompatActivity {

    private BottomBarLayout actMainBar;
    private FragmentManager fragmentManager;
    private ArrayList<Fragment> fragments;
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Shopping);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate: ");
        initbar();
        initView();

        initData();


    }

    private void initbar() {
        ImmersionBar.with(this)
                //状态栏颜色
                .statusBarColor(R.color.black)
                //导航栏颜色
                .navigationBarColor(R.color.white)
                //状态栏重叠问题
                .fitsSystemWindows(true)
                //导航栏状态栏自动变色
                .autoDarkModeEnable(true)
                //应用
                .init();
    }

    private void initView() {
        actMainBar = (BottomBarLayout) findViewById(R.id.act_main_bar);
        fragmentManager = getSupportFragmentManager();

        fragments = new ArrayList<>();

        Fragment fragment = (Fragment) ARouter.getInstance().build(RouterPath.Home.PAGER_HOME).navigation();
        Fragment fragment1 = (Fragment) ARouter.getInstance().build(RouterPath.Classify.PAGER_CLASSIFY).navigation();
        Fragment fragment2 = (Fragment) ARouter.getInstance().build(RouterPath.Shop.PAGER_SHOP).navigation();
        Fragment fragment3 = (Fragment) ARouter.getInstance().build(RouterPath.Message.PAGER_MESSAGE).navigation();
        Fragment fragment4 = (Fragment) ARouter.getInstance().build(RouterPath.My.PAGER_MY).navigation();


        fragments.add(fragment);
        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);
        fragments.add(fragment4);
        //接受  eventBus注册
        EventBus.getDefault().register(this);
        fragmentManager.beginTransaction().add(R.id.container1, fragments.get(0)).commit();
    }

    private void initData() {
        actMainBar.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(BottomBarItem bottomBarItem, int previousPosition, int currentPosition) {
                switch (currentPosition){
                    case 0:
                        fragmentManager.beginTransaction().replace(R.id.container1, fragments.get(currentPosition)).commitNowAllowingStateLoss();
                        break;
                    case 1:
                        fragmentManager.beginTransaction().replace(R.id.container1, fragments.get(currentPosition)).commitNowAllowingStateLoss();
                        break;
                    case 2:
                        fragmentManager.beginTransaction().replace(R.id.container1, fragments.get(currentPosition)).commitNowAllowingStateLoss();
                        break;
                    case 3:
                        fragmentManager.beginTransaction().replace(R.id.container1, fragments.get(currentPosition)).commitNowAllowingStateLoss();
                        break;
                    case 4:
                        fragmentManager.beginTransaction().replace(R.id.container1, fragments.get(currentPosition)).commitNowAllowingStateLoss();
                        break;
                }
            }
        });



    }

    @Subscribe
    public void inEven(String event){
        if (event.equals("111")){
            ToastUtils.showShort("2222");
            actMainBar.setCurrentItem(2);
//            fragmentManager.beginTransaction().replace(R.id.container1,fragments.get(2)).commitAllowingStateLoss();
//            fragmentManager.beginTransaction().hide(fragments.get(0))

        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        actMainBar.setUnread(2, GoodDatabase.getInstance(this).getGoodDao().getAllGood().size());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}